﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DTO;
using System.Collections.Generic;
using HRProject.Code.Common;

namespace HRProject.Code.DAO
{
    public class MonthlyWorktimeDAO : BaseDAO
    {
        public MonthlyWorktimeDAO()
            : base ()
        {
        }

        public BLMonthlyWorktimeSheet Collect(BLJobTitle jobTitleFilter, Guid agentId, string fullName, DateTime month, int totalPage, int pageSize, int pageIndex)
        {
            int startIndex = (pageIndex - 1) * pageSize + 1;
            if (pageIndex > totalPage)
            {
                startIndex = (totalPage - 1) * pageSize + 1;
            }
            else if (pageIndex < 1)
            {
                startIndex = 1;
            }
            int endIndex = startIndex + pageSize - 1;

            BLMonthlyWorktimeSheet result = new BLMonthlyWorktimeSheet(month.Year, month.Month);
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@jobTitleId", jobTitleFilter.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@agentId", agentId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@agentFullName", fullName, ParameterDirection.Input));
            condition.Add(new BaseCondition("@date", month, ParameterDirection.Input));
            condition.Add(new BaseCondition("@startIndex", startIndex, ParameterDirection.Input));
            condition.Add(new BaseCondition("@endIndex", endIndex, ParameterDirection.Input));
            DataTable data = select("usp_CollectMonthlyWorktimes", condition);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                Guid rAgentId = (Guid)data.Rows[i]["AgentId"];

                BLMonthlyWorktime agentWorktime = null;
                foreach (BLMonthlyWorktime mw in result.AgentsWorktime)
                {
                    if (mw.AgentId.Equals(rAgentId))
                    {
                        agentWorktime = mw;
                        break;
                    }
                }
                if (agentWorktime == null)
                {
                    agentWorktime = new BLMonthlyWorktime(rAgentId, month.Year, month.Month);
                    result.AgentsWorktime.Add(agentWorktime);
                }

                DateTime date = (DateTime)data.Rows[i]["Date"];
                agentWorktime.AgentFullName = (string)data.Rows[i]["FullName"];
                agentWorktime.DaysWorktime[date.Day - 1].AgentFullName = agentWorktime.AgentFullName;
                agentWorktime.DaysWorktime[date.Day - 1].LeaveMorning = (int)data.Rows[i]["LeaveMorning"];
                agentWorktime.DaysWorktime[date.Day - 1].LeaveAfternoon = (int)data.Rows[i]["LeaveAfternoon"];
                agentWorktime.DaysWorktime[date.Day - 1].DataSignature = (long)data.Rows[i]["DataSignature"];
            }
            return result;
        }

        public BLMonthlyWorktimeSheet CollectOverWorktime(BLJobTitle jobTitleFilter, Guid agentId, string fullName, DateTime month, int totalPage, int pageSize, int pageIndex)
        {
            int startIndex = (pageIndex - 1) * pageSize + 1;
            if (pageIndex > totalPage)
            {
                startIndex = (totalPage - 1) * pageSize + 1;
            }
            else if (pageIndex < 1)
            {
                startIndex = 1;
            }
            int endIndex = startIndex + pageSize - 1;

            BLMonthlyWorktimeSheet result = new BLMonthlyWorktimeSheet(month.Year, month.Month);
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@jobTitleId", jobTitleFilter.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@agentId", agentId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@agentFullName", fullName, ParameterDirection.Input));
            condition.Add(new BaseCondition("@date", month, ParameterDirection.Input));
            condition.Add(new BaseCondition("@startIndex", startIndex, ParameterDirection.Input));
            condition.Add(new BaseCondition("@endIndex", endIndex, ParameterDirection.Input));
            DataTable data = select("usp_CollectMonthlyOverWorktimes", condition);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                Guid rAgentId = (Guid)data.Rows[i]["AgentId"];

                BLMonthlyWorktime agentWorktime = null;
                foreach (BLMonthlyWorktime mw in result.AgentsWorktime)
                {
                    if (mw.AgentId.Equals(rAgentId))
                    {
                        agentWorktime = mw;
                        break;
                    }
                }
                if (agentWorktime == null)
                {
                    agentWorktime = new BLMonthlyWorktime(rAgentId, month.Year, month.Month);
                    result.AgentsWorktime.Add(agentWorktime);
                }

                DateTime date = (DateTime)data.Rows[i]["Date"];
                agentWorktime.AgentFullName = (string)data.Rows[i]["FullName"];
                agentWorktime.DaysWorktime[date.Day - 1].AgentFullName = agentWorktime.AgentFullName;
                Guid overWorktimeId = (Guid)data.Rows[i]["OverWorktimeId"];
                if (!Guid.Empty.Equals(overWorktimeId))
                {
                    BLOverWorktime overWorktime = new BLOverWorktime();
                    overWorktime.Id = overWorktimeId;
                    overWorktime.StartTime = (string)data.Rows[i]["StartTime"];
                    overWorktime.EndTime = (string)data.Rows[i]["EndTime"];
                    overWorktime.DataSignature = (long)data.Rows[i]["DataSignature"];
                    agentWorktime.DaysWorktime[date.Day - 1].OverWorktimes.Add(overWorktime);
                }
            }
            return result;
        }
    }
}
